package com.auto.learning.fragment;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.activity.SearchActivity;
import com.auto.learning.adapter.SearchComprehensiveListAdapter;
import com.auto.learning.bean.SearchBean;

public class SearchComprehensiveFragment extends Fragment {

	private ListView listview_comprehensive;
	private SearchComprehensiveListAdapter comprehensiveListAdapter;
	private TextView tv_data_empty;

	private SearchBean searchBean;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				String type = (String) msg.obj;
				((SearchActivity)getActivity()).setCurrentPage(type);
				break;
			}
		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_s_comprehensive, null);
		initView(view);
		return view;
	}

	private void initView(View view) {
		searchBean = new SearchBean();
		listview_comprehensive = (ListView) view.findViewById(R.id.listview_comprehensive);
		comprehensiveListAdapter = new SearchComprehensiveListAdapter(getActivity(), searchBean,handler);
		listview_comprehensive.setAdapter(comprehensiveListAdapter);
		tv_data_empty = (TextView) view.findViewById(R.id.tv_data_empty);
	}
	
	@Override
	public void onResume() {
		
		super.onResume();
	}

	public SearchBean getSearchBean() {
		return searchBean;
	}
	
	private <T> void clear(ArrayList<T> t){
		if (t != null && t.size() > 0) {
			t.clear();
		}
	}
	
	private <T> boolean checkIsEmpty(ArrayList<T> t){
		if (t == null || (t != null && t.size() == 0)) {
			return true;
		}
		return false;
	}

	public void setSearchBean(SearchBean m_searchBean) {
		clear(searchBean.getHofModel());
		clear(searchBean.getBookModel());
		clear(searchBean.getAnchorModel());
		if (m_searchBean.getBookModel() != null && m_searchBean.getBookModel().size() > 0) {
			searchBean.setBookModel(add(m_searchBean.getBookModel()));
		}
		if (m_searchBean.getAnchorModel() != null && m_searchBean.getAnchorModel().size() > 0) {
			searchBean.setAnchorModel(add(m_searchBean.getAnchorModel()));
		}
		if (m_searchBean.getHofModel() != null && m_searchBean.getHofModel().size() > 0) {
			searchBean.setHofModel(add(m_searchBean.getHofModel()));
		}
		if (comprehensiveListAdapter != null) {
			comprehensiveListAdapter.notifyDataSetChanged();
		}
		if (checkIsEmpty(searchBean.getAnchorModel()) && checkIsEmpty(searchBean.getBookModel()) && checkIsEmpty(searchBean.getHofModel())) {
			listview_comprehensive.setVisibility(View.GONE);
			tv_data_empty.setVisibility(View.VISIBLE);
		}else {
			listview_comprehensive.setVisibility(View.VISIBLE);
			tv_data_empty.setVisibility(View.GONE);
		}
	}
	
	private <T> ArrayList<T> add(ArrayList<T> list){
		ArrayList<T> temp = new ArrayList<T>();
		for (T t : list) {
			if (list.indexOf(t) < 3) 
				temp.add(t);
			else 
				break;
		}
		return temp;
	}
	

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	
	public void changePlayStute(boolean isPlaying){
		if (comprehensiveListAdapter != null) {
			comprehensiveListAdapter.notifyDataSetChanged();
		}
	}
}
